using System;
using System.Collections.Generic;
using System.Data;
using System.Web.UI.WebControls;
using Framework.Library.Base.Base;
using Framework.Library.Base.DataAccess;
using Framework.Library.Base.Extensions;
using Framework.Library.Core.Utils;
using Framework.Library.Core.Utils.GlobalHelper;
using Framework.Library.Data.DAL;
using Framework.Library.Data.DAL.JObject;
using Framework.Library.Data.DAO;
using ServiceStack.Text;
using System.IO;
using System.Collections;
using Framework.Library.Core.Config;

namespace Framework.Portal.Admin.Modules.Menus
{
    public partial class MenuManager : BaseControl
    {
        private static List<JMenuType> lstType = new List<JMenuType>();
        private static List<JMenuExt> lstExt = new List<JMenuExt>();
        private int curMenuBinding;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                btnExtension.Enabled = false;
                btnType.Enabled = false;
                btnModule.Enabled = false;
                btnJavascript.Enabled = false;
                btnStyleSheet.Enabled = false;
                List<PortalMenu> dt = ManagerFactory.PortalMenuManager.GetAll(Session.WorkingPortal().PORTALID);
                if (dt != null)
                {
                    var rootItem = new ListItem("Root", "0");
                    General_drpParentID.Items.Add(rootItem);
                    PortalAccess.LoadMenus(dt, General_drpParentID.Items, 0, 0);
                    PortalAccess.LoadMenus(dt, tvwCmds.Nodes);
                }
                else
                    General_drpParentID.Items.Add(new ListItem("Root", "0"));

                MiscUtility.FillIndex(General_drpPriority, 30, 1);
                
                LoadPlaceholder(Module_drpPlaceholder.Items);

                List<PortalControl> dtControls = ManagerFactory.DAOEntity.GetList(new PortalControl() { PORTALID = Session.WorkingPortal().PORTALID });
                if (dtControls != null)
                    LoadControlItems(dtControls);
                else
                    Module_drpControl.Items.Add(new ListItem("None", "0"));
                var curTheme = JsonSerializer.DeserializeFromString<JTheme>(Session.WorkingPortal().THEME);
                if (curTheme != null)
                {
                    rptJavascriptPortal.DataSource = (curTheme.js != null) ? curTheme.js : null;
                    rptJavascriptPortal.DataBind();
                    rptStyleSheetPortal.DataSource = (curTheme.css != null) ? curTheme.css : null;
                    rptStyleSheetPortal.DataBind();
                }
                var curModule = JsonSerializer.DeserializeFromString<List<JSection>>(Session.WorkingPortal().MASTERPAGE);
                if (curModule != null)
                {
                    rptModulePortal.DataSource = (curModule != null) ? curModule : null;
                    rptModulePortal.DataBind();
                }
            }
        }

        protected void tvwCmds_SelectedNodeChanged(object sender, EventArgs e)
        {
            try
            {
                lstExt.Clear();
                lstType.Clear();
                curMenuBinding = Convert.ToInt32(tvwCmds.SelectedValue);
                PortalMenu info = ManagerFactory.PortalMenuManager.GetInfo(Session.WorkingPortal().PORTALID, curMenuBinding);
                if (info != null)
                {
                    btnExtension.Enabled = true;
                    btnType.Enabled = true;
                    btnModule.Enabled = true;
                    btnJavascript.Enabled = true;
                    btnStyleSheet.Enabled = true;
                    General_lblID.Text = info.ID.ToString();
                    General_drpParentID.SelectedValue = info.PARENTID.ToString();
                    General_txtName.Text = info.NAME;
                    General_txtDescription.Text = info.DESCRIPTION;
                    General_txtImage.Text = info.IMAGE;
                    General_drpLinkType.SelectedValue = info.LINKTYPE.ToString();
                    General_txtParametter.Text = info.PARAMETTERS;
                    General_drpPriority.SelectedValue = info.PRIORITY.ToString();
                    General_chkAuthenticate.Checked = Convert.ToBoolean(info.SA);
                    General_chkEnable.Checked = Convert.ToBoolean(info.ENABLE);
                    General_chkVisible.Checked = Convert.ToBoolean(info.VISIBLE);
                    General_chkHomepage.Checked = Convert.ToBoolean(info.HOMEPAGE);
                    if (!string.IsNullOrEmpty(info.MENULAYOUT))
                    {
                        lstType = JsonSerializer.DeserializeFromString<List<JMenuType>>(info.MENULAYOUT);
                        rptType.DataSource = lstType;
                        rptType.DataBind();
                        Module_drpMenutype.Items.Clear();
                        Javascript_drpMenutype.Items.Clear();
                        StyleSheet_drpMenutype.Items.Clear();
                        int index = 0;
                        foreach (JMenuType jMenuType in lstType)
                        {
                            Module_drpMenutype.Items.Add(new ListItem(jMenuType.name, index.ToString()));
                            Javascript_drpMenutype.Items.Add(new ListItem(jMenuType.name, index.ToString()));
                            StyleSheet_drpMenutype.Items.Add(new ListItem(jMenuType.name, index.ToString()));
                            index++;                            
                        }
                        if (Module_drpMenutype.Items.Count > 0)
                        {
                            LoadSection();
                            LoadJavascript();
                            LoadStyleSheet();
                            StyleSheet_drpControl.Items.Clear();
                            Javascript_drpControl.Items.Clear();
                            StyleSheet_drpControl.DataSource = GetFiles("css", "css");
                            StyleSheet_drpControl.DataBind();
                            Javascript_drpControl.DataSource = GetFiles("javascript", "js");
                            Javascript_drpControl.DataBind();
                        }
                            
                    }
                    else
                    {
                        Module_drpMenutype.Items.Clear();
                        Javascript_drpMenutype.Items.Clear();
                        StyleSheet_drpMenutype.Items.Clear();
                        rptType.DataSource = null;
                        rptType.DataBind();
                    }
                    if (!string.IsNullOrEmpty(info.PREIMAGE))
                    {
                        lstExt = JsonSerializer.DeserializeFromString<List<JMenuExt>>(info.PREIMAGE);
                        rptExtension.DataSource = lstExt;
                        rptExtension.DataBind();
                    }
                    else
                    {
                        rptExtension.DataSource = null;
                        rptExtension.DataBind();
                    }
                }
                else
                {
                    General_lblID.Text = "0";
                }

            }
            catch (Exception exception)
            {
                General_lblUpdateStatus.Text = exception.Message + ": " + exception.StackTrace;
            }
        }

        protected void General_cmdUpdate_Click(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(General_lblID.Text) && Page.IsValid)
            {
                var menu = new PortalMenu();
                menu.PARENTID = Convert.ToInt32(General_drpParentID.SelectedValue);
                menu.PORTALID = Session.WorkingPortal().PORTALID;
                menu.NAME = General_txtName.Text;
                menu.DESCRIPTION = General_txtDescription.Text;
                menu.IMAGE = General_txtImage.Text;
                menu.LINKTYPE = Convert.ToInt32(General_drpLinkType.SelectedValue);
                if (menu.LINKTYPE == 0)
                    menu.PARAMETTERS = null;
                else
                    menu.PARAMETTERS = General_txtParametter.Text;
                menu.PRIORITY = Convert.ToInt32(General_drpPriority.SelectedValue);
                menu.VISIBLE = Convert.ToInt32(General_chkVisible.Checked);
                menu.ENABLE = Convert.ToInt32(General_chkEnable.Checked);
                menu.SA = Convert.ToInt32(General_chkAuthenticate.Checked);
                menu.HOMEPAGE = Convert.ToInt32(General_chkHomepage.Checked);
                menu.ID = Convert.ToInt32(General_lblID.Text);
                ManagerFactory.DAOEntity.Update(menu);
                General_lblUpdateStatus.Text = Localization.GetText("MESSAGE", "MSG_UPDATE_SUCCESS");
                General_GetDefault();
            }
        }

        protected void General_cmdAddNew_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                var menu = new PortalMenu();
                menu.PARENTID = Convert.ToInt32(General_drpParentID.SelectedValue);
                menu.PORTALID = Session.WorkingPortal().PORTALID;
                menu.NAME = General_txtName.Text;
                menu.DESCRIPTION = General_txtDescription.Text;
                menu.IMAGE = General_txtImage.Text;
                menu.LINKTYPE = Convert.ToInt32(General_drpLinkType.SelectedValue);
                if (menu.LINKTYPE == 0)
                    menu.PARAMETTERS = null;
                else
                    menu.PARAMETTERS = General_txtParametter.Text;
                menu.PRIORITY = Convert.ToInt32(General_drpPriority.SelectedValue);
                menu.VISIBLE = Convert.ToInt32(General_chkVisible.Checked);
                menu.ENABLE = Convert.ToInt32(General_chkEnable.Checked);
                menu.SA = Convert.ToInt32(General_chkAuthenticate.Checked);
                menu.HOMEPAGE = Convert.ToInt32(General_chkHomepage.Checked);
                menu.LANGID = 0;
                menu.MENULAYOUT =
                    JsonSerializer.SerializeToString(new JMenuType {description = string.Empty, name = "default"});
                ManagerFactory.DAOEntity.Insert(menu);
                General_lblUpdateStatus.Text = Localization.GetText("MESSAGE", "MSG_UPDATE_SUCCESS");
                General_GetDefault();
            }
        }

        protected void General_cmdEmpty_Click(object sender, EventArgs e)
        {
            General_GetDefault();
        }

        protected void General_cmdDelete_Click(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(General_lblID.Text))
            {
                ManagerFactory.PortalMenuManager.Delete(Convert.ToInt32(General_lblID.Text));
                General_lblUpdateStatus.Text = Localization.GetText("MESSAGE", "MSG_UPDATE_SUCCESS");
                General_GetDefault();
            }
        }

        private void General_GetDefault()
        {
            List<PortalMenu> dt = ManagerFactory.PortalMenuManager.GetAll(Session.WorkingPortal().PORTALID);
            if (dt != null)
            {
                General_drpParentID.Items.Clear();
                var rootItem = new ListItem("Root", "0");
                General_drpParentID.Items.Add(rootItem);
                PortalAccess.LoadMenus(dt, General_drpParentID.Items, 0, 0);
                PortalAccess.LoadMenus(dt, tvwCmds.Nodes);
            }
            else
                General_drpParentID.Items.Add(new ListItem("Root", "0"));
            General_lblID.Text = string.Empty;
            General_drpParentID.SelectedIndex = -1;
            General_txtName.Text = string.Empty;
            General_txtDescription.Text = string.Empty;
            General_txtImage.Text = string.Empty;
            General_drpLinkType.SelectedIndex = -1;
            General_txtParametter.Text = string.Empty;
            General_drpPriority.SelectedIndex = -1;
            General_chkEnable.Checked = true;
            General_chkVisible.Checked = true;
            General_chkHomepage.Checked = false;
            General_chkAuthenticate.Checked = false;
            Module_drpMenutype.Items.Clear();
            Javascript_drpMenutype.Items.Clear();
            StyleSheet_drpMenutype.Items.Clear();
            rptType.DataSource = null;
            rptType.DataBind();
            rptExtension.DataSource = null;
            rptExtension.DataBind();
            rptModule.DataSource = null;
            rptModule.DataBind();
        }

        protected void General_drpLinkType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (General_drpLinkType.SelectedValue == "0")
                General_txtParametter.Enabled = false;
            else
                General_txtParametter.Enabled = true;
        }

        protected void rptExtension_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                var info = (JMenuExt) e.Item.DataItem;
                var lblName = (Label) e.Item.FindControl("lblName");
                var lblSTT = (Label) e.Item.FindControl("lblSTT");
                var btnEdit = (ImageButton) e.Item.FindControl("Label17");
                btnEdit.CommandName = "edit";
                btnEdit.CommandArgument = e.Item.ItemIndex.ToString();

                var btnDelete = (ImageButton) e.Item.FindControl("Label7");
                btnDelete.CommandName = "delete";
                btnDelete.CommandArgument = e.Item.ItemIndex.ToString();

                lblSTT.Text = (e.Item.ItemIndex + 1).ToString();
                lblName.Text = info.title;
            }
        }

        public void rptExtension_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            int cur = Convert.ToInt32(e.CommandArgument);
            switch (e.CommandName)
            {
                case "edit":
                    Extension_lblID.Text = cur.ToString();
                    Extension_txtName.Text = lstExt[cur].title;
                    Extension_txtDescription.Text = lstExt[cur].description;
                    Extension_txtTitImage.Text = lstExt[cur].thumnail;
                    Extension_txtDeaImage.Text = lstExt[cur].listimages;
                    break;
                case "delete":
                    lstExt.RemoveAt(cur);
                    ManagerFactory.DAOEntity.Update(new PortalMenu() { ID = Convert.ToInt32(General_lblID.Text), PREIMAGE = JsonSerializer.SerializeToString(lstExt) });
                    rptExtension.DataSource = lstExt;
                    rptExtension.DataBind();
                    Extension_GetDefault();
                    break;
                default:
                    break;
            }
        }

        protected void Extension_cmdUpdate_Click(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(Extension_lblID.Text) && Page.IsValid)
            {
                int Id = Convert.ToInt32(General_lblID.Text);
                int typeId = Convert.ToInt32(Extension_lblID.Text);
                var ext = new JMenuExt();
                ext.description = Extension_txtDescription.Text;
                ext.title = Extension_txtName.Text;
                ext.thumnail = Extension_txtTitImage.Text;
                ext.listimages = Extension_txtDeaImage.Text;
                lstExt[typeId] = ext;                
                string PREIMAGE = JsonSerializer.SerializeToString(lstExt);
                ManagerFactory.DAOEntity.Update(new PortalMenu() { ID = Id, PREIMAGE = PREIMAGE });
                Extension_lblUpdateStatus.Text = Localization.GetText("MESSAGE", "MSG_UPDATE_SUCCESS");
                rptExtension.DataSource = lstExt;
                rptExtension.DataBind();
                Extension_GetDefault();
            }
        }

        protected void Extension_cmdAddNew_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                int Id = Convert.ToInt32(General_lblID.Text);
                var ext = new JMenuExt();
                ext.description = Extension_txtDescription.Text;
                ext.title = Extension_txtName.Text;
                ext.thumnail = Extension_txtTitImage.Text;
                ext.listimages = Extension_txtDeaImage.Text;
                lstExt.Add(ext);                
                string PREIMAGE = JsonSerializer.SerializeToString(lstExt);
                ManagerFactory.DAOEntity.Update(new PortalMenu() { ID = Id, PREIMAGE = PREIMAGE });
                Extension_lblUpdateStatus.Text = Localization.GetText("MESSAGE", "MSG_UPDATE_SUCCESS");
                rptExtension.DataSource = lstExt;
                rptExtension.DataBind();
                Extension_GetDefault();
            }
        }

        protected void Extension_cmdEmpty_Click(object sender, EventArgs e)
        {
            Extension_GetDefault();
        }

        private void Extension_GetDefault()
        {
            Extension_txtDescription.Text = string.Empty;
            Extension_txtName.Text = string.Empty;
            Extension_txtTitImage.Text = string.Empty;
            Extension_txtDeaImage.Text = string.Empty;
        }

        protected void rptType_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                var info = (JMenuType) e.Item.DataItem;
                var lblName = (Label) e.Item.FindControl("lblName");
                var lblSTT = (Label) e.Item.FindControl("lblSTT");
                lblSTT.Text = (e.Item.ItemIndex + 1).ToString();
                var btnEdit = (ImageButton) e.Item.FindControl("Label17");
                btnEdit.CommandName = "edit";
                btnEdit.CommandArgument = e.Item.ItemIndex.ToString();

                var btnDelete = (ImageButton) e.Item.FindControl("Label7");
                btnDelete.CommandName = "delete";
                btnDelete.CommandArgument = e.Item.ItemIndex.ToString();
                lblName.Text = info.name;
            }
        }

        public void rptType_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            int cur = Convert.ToInt32(e.CommandArgument);
            switch (e.CommandName)
            {
                case "edit":
                    Type_lblID.Text = cur.ToString();
                    Type_txtName.Text = lstType[cur].name;
                    Type_txtDescription.Text = lstType[cur].description;
                    break;
                case "delete":
                    lstType.RemoveAt(cur);
                    ManagerFactory.DAOEntity.Update(new PortalMenu() { ID = Convert.ToInt32(General_lblID.Text), MENULAYOUT = JsonSerializer.SerializeToString(lstType) });
                    rptType.DataSource = lstType;
                    rptType.DataBind();
                    int index = 0;
                    Module_drpMenutype.Items.Clear();
                    StyleSheet_drpMenutype.Items.Clear();
                    Javascript_drpMenutype.Items.Clear();
                    foreach (JMenuType jMenuType in lstType)
                    {
                        index++;
                        Module_drpMenutype.Items.Add(new ListItem(jMenuType.name, index.ToString()));
                        Javascript_drpMenutype.Items.Add(new ListItem(jMenuType.name, index.ToString()));
                        StyleSheet_drpMenutype.Items.Add(new ListItem(jMenuType.name, index.ToString()));
                    }
                    Type_GetDefault();
                    break;
                default:
                    break;
            }
        }

        protected void Type_cmdUpdate_Click(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(Type_lblID.Text) && Page.IsValid)
            {
                int Id = Convert.ToInt32(General_lblID.Text);
                int typeId = Convert.ToInt32(Type_lblID.Text);
                var ext = lstType[typeId];
                ext.description = Type_txtDescription.Text;
                ext.name = Type_txtName.Text;
                lstType[typeId] = ext;
                string MENULAYOUT = JsonSerializer.SerializeToString(lstType);
                ManagerFactory.DAOEntity.Update(new PortalMenu() { ID = Id, MENULAYOUT = MENULAYOUT });
                Type_lblUpdateStatus.Text = Localization.GetText("MESSAGE", "MSG_UPDATE_SUCCESS");
                rptType.DataSource = lstType;
                rptType.DataBind();
                int index = 0;
                Module_drpMenutype.Items.Clear();
                Javascript_drpMenutype.Items.Clear();
                StyleSheet_drpMenutype.Items.Clear();
                foreach (JMenuType jMenuType in lstType)
                {
                    Module_drpMenutype.Items.Add(new ListItem(jMenuType.name, index.ToString()));
                    Javascript_drpMenutype.Items.Add(new ListItem(jMenuType.name, index.ToString()));
                    StyleSheet_drpMenutype.Items.Add(new ListItem(jMenuType.name, index.ToString()));
                    index++;
                }
                Type_GetDefault();
            }
        }

        protected void Type_cmdAddNew_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                int Id = Convert.ToInt32(General_lblID.Text);
                var ext = new JMenuType();
                ext.description = Type_txtDescription.Text;
                ext.name = Type_txtName.Text;
                lstType.Add(ext);
                string MENULAYOUT = JsonSerializer.SerializeToString(lstType);
                ManagerFactory.DAOEntity.Update(new PortalMenu() { ID = Id, MENULAYOUT = MENULAYOUT });
                Type_lblUpdateStatus.Text = Localization.GetText("MESSAGE", "MSG_UPDATE_SUCCESS");
                rptType.DataSource = lstType;
                rptType.DataBind();
                int index = 0;
                Module_drpMenutype.Items.Clear();
                Javascript_drpMenutype.Items.Clear();
                StyleSheet_drpMenutype.Items.Clear();
                foreach (JMenuType jMenuType in lstType)
                {
                    Module_drpMenutype.Items.Add(new ListItem(jMenuType.name, index.ToString()));
                    Javascript_drpMenutype.Items.Add(new ListItem(jMenuType.name, index.ToString()));
                    StyleSheet_drpMenutype.Items.Add(new ListItem(jMenuType.name, index.ToString()));
                    index++;
                }
                Type_GetDefault();
            }
        }

        protected void Type_cmdEmpty_Click(object sender, EventArgs e)
        {
            Type_GetDefault();
        }

        private void Type_GetDefault()
        {
            Type_lblID.Text = string.Empty;
            Type_txtName.Text = string.Empty;
            Type_txtDescription.Text = string.Empty;
        }


        protected void rptModulePortal_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                var info = (JSection) e.Item.DataItem;
                var lblName = (Label) e.Item.FindControl("lblName");
                var lblSTT = (Label) e.Item.FindControl("lblSTT");
                var lblPlaceholder = (Label)e.Item.FindControl("lblPlaceholder");
                lblSTT.Text = (e.Item.ItemIndex + 1).ToString();
                lblName.Text = info.title;
                lblPlaceholder.Text = info.placeholder;
            }
        }

        protected void rptModule_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                var info = (JSection) e.Item.DataItem;
                var lblName = (Label) e.Item.FindControl("lblName");
                var lblSTT = (Label) e.Item.FindControl("lblSTT");
                var lblPlaceholder = (Label)e.Item.FindControl("lblPlaceholder");
                lblSTT.Text = (e.Item.ItemIndex + 1).ToString();
                lblName.Text = info.title;
                lblPlaceholder.Text = info.placeholder;

                var btnEdit = (ImageButton) e.Item.FindControl("Label17");
                btnEdit.CommandName = "edit";
                btnEdit.CommandArgument = e.Item.ItemIndex.ToString();

                var btnDelete = (ImageButton) e.Item.FindControl("Label7");
                btnDelete.CommandName = "delete";
                btnDelete.CommandArgument = e.Item.ItemIndex.ToString();
            }
        }

        public void rptModule_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            int cur = Convert.ToInt32(e.CommandArgument);
            JMenuType info = lstType[Convert.ToInt32(Module_drpMenutype.SelectedValue)];
            if (info != null)
            {
                List<JSection> lstSection = info.control;
                if (lstSection.Count > 0)
                {
                    switch (e.CommandName)
                    {
                        case "edit":
                            Module_lblID.Text = e.CommandArgument.ToString();
                            Module_txtTitle.Text = lstSection[cur].title;
                            Module_drpPlaceholder.SelectedValue = lstSection[cur].placeholder;
                            Module_txtParameter.Text = lstSection[cur].parameter;
                            Module_chkShowtitle.Checked = Convert.ToBoolean(lstSection[cur].showtile);
                            ListItem item = Module_drpControl.Items.FindByValue(lstSection[cur].control);

                            if (item != null)
                                Module_drpControl.SelectedValue = lstSection[cur].control;
                            else
                                Module_drpControl.SelectedIndex = -1;
                            Module_chkSA.Checked = Convert.ToBoolean(lstSection[cur].sa);
                            break;
                        case "delete":
                            lstSection.RemoveAt(cur);
                            info.control = lstSection;
                            lstType[Convert.ToInt32(Module_drpMenutype.SelectedValue)] = info;
                            string MENULAYOUT = JsonSerializer.SerializeToString(lstType);
                            ManagerFactory.DAOEntity.Update(new PortalMenu() { ID = Convert.ToInt32(General_lblID.Text), MENULAYOUT = MENULAYOUT });
                            Module_lblUpdateStatus.Text = Localization.GetText("MESSAGE", "MSG_UPDATE_SUCCESS");
                            LoadSection();
                            Module_GetDefault();
                            break;
                        default:
                            break;
                    }
                }

            }
        }

        protected void Module_cmdUpdate_Click(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(Module_lblID.Text) && Page.IsValid)
            {
                JMenuType info = lstType[Convert.ToInt32(Module_drpMenutype.SelectedValue)];
                if (info != null)
                {
                    List<JSection> lstSection = info.control;
                    int curModule = Convert.ToInt32(Module_lblID.Text);
                    lstSection[curModule].control = Module_drpControl.SelectedValue;
                    lstSection[curModule].placeholder = Module_drpPlaceholder.SelectedValue;
                    lstSection[curModule].title = Module_txtTitle.Text;
                    lstSection[curModule].showtile = Module_chkShowtitle.Checked;
                    lstSection[curModule].parameter = Module_txtParameter.Text;
                    lstSection[curModule].sa = Convert.ToInt32(Module_chkSA.Checked);
                    info.control = lstSection;
                    lstType[Convert.ToInt32(Module_drpMenutype.SelectedValue)] = info;
                    string MENULAYOUT = JsonSerializer.SerializeToString(lstType);
                    ManagerFactory.DAOEntity.Update(new PortalMenu() { ID = Convert.ToInt32(General_lblID.Text), MENULAYOUT = MENULAYOUT });
                    Module_lblUpdateStatus.Text = Localization.GetText("MESSAGE", "MSG_UPDATE_SUCCESS");
                    LoadSection();
                    Module_GetDefault();
                }
            }
        }

        protected void Module_cmdAddNew_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                JMenuType info = lstType[Convert.ToInt32(Module_drpMenutype.SelectedValue)];
                if (info != null)
                {
                    List<JSection> lstSection = info.control;
                    if (lstSection == null)
                        lstSection = new List<JSection>();
                    JSection infoSection = new JSection();
                    infoSection.control = Module_drpControl.SelectedValue;
                    infoSection.placeholder = Module_drpPlaceholder.SelectedValue;
                    infoSection.title = Module_txtTitle.Text;
                    infoSection.showtile = Module_chkShowtitle.Checked;
                    infoSection.parameter = Module_txtParameter.Text;
                    infoSection.sa = Convert.ToInt32(Module_chkSA.Checked);
                    lstSection.Add(infoSection);
                    info.control = lstSection;
                    lstType[Convert.ToInt32(Module_drpMenutype.SelectedValue)] = info;
                    string MENULAYOUT = JsonSerializer.SerializeToString(lstType);
                    ManagerFactory.DAOEntity.Update(new PortalMenu() { ID = Convert.ToInt32(General_lblID.Text), MENULAYOUT = MENULAYOUT });
                    Module_lblUpdateStatus.Text = Localization.GetText("MESSAGE", "MSG_UPDATE_SUCCESS");
                    LoadSection();
                    Module_GetDefault();
                }
            }
        }

        protected void Module_cmdEmpty_Click(object sender, EventArgs e)
        {
            Module_GetDefault();
        }

        protected void Module_drpMenutype_SelectedIndexChanged(object sender, EventArgs e)
        {
            int moduleID = Convert.ToInt32(Module_drpMenutype.SelectedValue);
            StyleSheet_drpMenutype.SelectedIndex = moduleID;
            Javascript_drpMenutype.SelectedIndex = moduleID;
            LoadSection();
            LoadJavascript();
            LoadStyleSheet();
        }

        protected void Javascript_drpMenutype_SelectedIndexChanged(object sender, EventArgs e)
        {
            int moduleID = Javascript_drpMenutype.SelectedIndex;
            StyleSheet_drpMenutype.SelectedIndex = moduleID;
            Module_drpMenutype.SelectedIndex = moduleID;
            LoadSection();
            LoadJavascript();
            LoadStyleSheet();
        }

        protected void StyleSheet_drpMenutype_SelectedIndexChanged(object sender, EventArgs e)
        {
            int moduleID = Convert.ToInt32(StyleSheet_drpMenutype.SelectedValue);
            Javascript_drpMenutype.SelectedIndex = moduleID;
            Module_drpMenutype.SelectedIndex = moduleID;
            LoadSection();
            LoadJavascript();
            LoadStyleSheet();
        }

        private void Module_GetDefault()
        {
            Module_lblID.Text = string.Empty;
            Module_txtTitle.Text = string.Empty;
            Module_drpPlaceholder.SelectedIndex = -1;
            Module_txtParameter.Text = string.Empty;
            Module_chkShowtitle.Checked = false;
            Module_drpControl.SelectedIndex = -1;
            Module_chkSA.Checked = false;
        }

        protected void Javascript_cmdUpdate_Click(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(Javascript_lblID.Text) && Page.IsValid)
            {
                JMenuType info = lstType[Convert.ToInt32(Module_drpMenutype.SelectedValue)];
                if (info != null)
                {
                    int curModule = Convert.ToInt32(Javascript_lblID.Text);
                    info.js[curModule] = Javascript_drpControl.SelectedValue;
                    lstType[Convert.ToInt32(Module_drpMenutype.SelectedValue)] = info;
                    string MENULAYOUT = JsonSerializer.SerializeToString(lstType);
                    ManagerFactory.DAOEntity.Update(new PortalMenu() { ID = Convert.ToInt32(General_lblID.Text), MENULAYOUT = MENULAYOUT });
                    Javascript_lblUpdateStatus.Text = Localization.GetText("MESSAGE", "MSG_UPDATE_SUCCESS");
                    LoadJavascript();
                    Javascript_GetDefault();
                }
            }
        }

        protected void Javascript_cmdAddNew_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                JMenuType info = lstType[Convert.ToInt32(Module_drpMenutype.SelectedValue)];
                if (info != null)
                {
                    List<string> lstJavascript = info.js;
                    if (lstJavascript == null)
                        lstJavascript = new List<string>();
                    lstJavascript.Add(Javascript_drpControl.SelectedValue);
                    info.js = lstJavascript;
                    lstType[Convert.ToInt32(Module_drpMenutype.SelectedValue)] = info;
                    string MENULAYOUT = JsonSerializer.SerializeToString(lstType);
                    ManagerFactory.DAOEntity.Update(new PortalMenu() { ID = Convert.ToInt32(General_lblID.Text), MENULAYOUT = MENULAYOUT });
                    Javascript_lblUpdateStatus.Text = Localization.GetText("MESSAGE", "MSG_UPDATE_SUCCESS");
                    LoadJavascript();
                    Javascript_GetDefault();
                }
            }
        }

        protected void Javascript_cmdEmpty_Click(object sender, EventArgs e)
        {
            Javascript_GetDefault();
        }

        private void Javascript_GetDefault()
        {
            Javascript_lblID.Text = string.Empty;
            Javascript_drpControl.SelectedIndex = -1;
        }

        protected void StyleSheet_cmdUpdate_Click(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(StyleSheet_lblID.Text) && Page.IsValid)
            {
                JMenuType info = lstType[Convert.ToInt32(Module_drpMenutype.SelectedValue)];
                if (info != null)
                {
                    int curModule = Convert.ToInt32(StyleSheet_lblID.Text);
                    info.css[curModule] = StyleSheet_drpControl.SelectedValue;
                    lstType[Convert.ToInt32(Module_drpMenutype.SelectedValue)] = info;
                    string MENULAYOUT = JsonSerializer.SerializeToString(lstType);
                    ManagerFactory.DAOEntity.Update(new PortalMenu() { ID = Convert.ToInt32(General_lblID.Text), MENULAYOUT = MENULAYOUT });
                    StyleSheet_lblUpdateStatus.Text = Localization.GetText("MESSAGE", "MSG_UPDATE_SUCCESS");
                    LoadStyleSheet();
                    StyleSheet_GetDefault();
                }
            }
        }

        protected void StyleSheet_cmdAddNew_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                JMenuType info = lstType[Convert.ToInt32(Module_drpMenutype.SelectedValue)];
                if (info != null)
                {
                    List<string> lstStyleSheet = info.css;
                    if (lstStyleSheet == null)
                        lstStyleSheet = new List<string>();
                    lstStyleSheet.Add(StyleSheet_drpControl.SelectedValue);
                    info.css = lstStyleSheet;
                    lstType[Convert.ToInt32(Module_drpMenutype.SelectedValue)] = info;
                    string MENULAYOUT = JsonSerializer.SerializeToString(lstType);
                    ManagerFactory.DAOEntity.Update(new PortalMenu() { ID = Convert.ToInt32(General_lblID.Text), MENULAYOUT = MENULAYOUT });
                    StyleSheet_lblUpdateStatus.Text = Localization.GetText("MESSAGE", "MSG_UPDATE_SUCCESS");
                    LoadStyleSheet();
                    StyleSheet_GetDefault();
                }
            }
        }

        protected void StyleSheet_cmdEmpty_Click(object sender, EventArgs e)
        {
            StyleSheet_GetDefault();
        }

        private void StyleSheet_GetDefault()
        {
            StyleSheet_lblID.Text = string.Empty;
            StyleSheet_drpControl.SelectedIndex = -1;
        }

        protected void rptJavascriptPortal_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                var info = (string)e.Item.DataItem;
                var lblName = (Label)e.Item.FindControl("lblName");
                var lblSTT = (Label)e.Item.FindControl("lblSTT");
                lblSTT.Text = (e.Item.ItemIndex + 1).ToString();
                lblName.Text = info;
            }
        }

        protected void rptJavascript_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                var info = (string)e.Item.DataItem;
                var lblName = (Label)e.Item.FindControl("lblName");
                var lblSTT = (Label)e.Item.FindControl("lblSTT");
                lblSTT.Text = (e.Item.ItemIndex + 1).ToString();
                lblName.Text = info;

                var btnEdit = (ImageButton)e.Item.FindControl("Label17");
                btnEdit.CommandName = "edit";
                btnEdit.CommandArgument = e.Item.ItemIndex.ToString();

                var btnDelete = (ImageButton)e.Item.FindControl("Label7");
                btnDelete.CommandName = "delete";
                btnDelete.CommandArgument = e.Item.ItemIndex.ToString();
            }
        }

        public void rptJavascript_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            int cur = Convert.ToInt32(e.CommandArgument);
            JMenuType info = lstType[Convert.ToInt32(Module_drpMenutype.SelectedValue)];
            if (info != null)
            {
                List<string> lstJavascript = info.js;
                if (lstJavascript.Count > 0)
                {
                    switch (e.CommandName)
                    {
                        case "edit":
                            Javascript_lblID.Text = e.CommandArgument.ToString();
                            Javascript_drpControl.SelectedValue = lstJavascript[cur];
                            break;
                        case "delete":
                            lstJavascript.RemoveAt(cur);
                            info.js = lstJavascript;
                            lstType[Convert.ToInt32(Module_drpMenutype.SelectedValue)] = info;
                            string MENULAYOUT = JsonSerializer.SerializeToString(lstType);
                            ManagerFactory.DAOEntity.Update(new PortalMenu() { ID = Convert.ToInt32(General_lblID.Text), MENULAYOUT = MENULAYOUT });
                            Javascript_lblUpdateStatus.Text = Localization.GetText("MESSAGE", "MSG_UPDATE_SUCCESS");
                            LoadJavascript();
                            Javascript_GetDefault();
                            break;
                        default:
                            break;
                    }
                }

            }
        }

        public void rptStyleSheet_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            int cur = Convert.ToInt32(e.CommandArgument);
            JMenuType info = lstType[Convert.ToInt32(Module_drpMenutype.SelectedValue)];
            if (info != null)
            {
                List<string> lstStyleSheet = info.css;
                if (lstStyleSheet.Count > 0)
                {
                    switch (e.CommandName)
                    {
                        case "edit":
                            StyleSheet_lblID.Text = e.CommandArgument.ToString();
                            StyleSheet_drpControl.SelectedValue = lstStyleSheet[cur];
                            break;
                        case "delete":
                            lstStyleSheet.RemoveAt(cur);
                            info.css = lstStyleSheet;
                            lstType[Convert.ToInt32(Module_drpMenutype.SelectedValue)] = info;
                            string MENULAYOUT = JsonSerializer.SerializeToString(lstType);
                            ManagerFactory.DAOEntity.Update(new PortalMenu() { ID = Convert.ToInt32(General_lblID.Text), MENULAYOUT = MENULAYOUT });
                            StyleSheet_lblUpdateStatus.Text = Localization.GetText("MESSAGE", "MSG_UPDATE_SUCCESS");
                            LoadStyleSheet();
                            StyleSheet_GetDefault();
                            break;
                        default:
                            break;
                    }
                }

            }
        }

        private ArrayList GetFiles(string dir, string ext)
        {
            ArrayList lstFile = new ArrayList();
            string rootPath = Configurations.ROOTDIR;
            string dirPath = Server.MapPath(string.Format("{0}Templates/{1}/Resources/{2}", Configurations.ROOTDIR, Session.WorkingPortal().LAYOUT, dir));
            if (Directory.Exists(dirPath))
            {
                string[] files = Directory.GetFiles(dirPath);
                for (int i = 0; i < files.Length; i++)
                {
                    if (files[i].EndsWith("." + ext))
                    {
                        string temp = files[i].Replace(Server.MapPath(rootPath), string.Empty);
                        temp = temp.Replace("\\", "/");
                        if (!temp.StartsWith("/")) temp = "/" + temp;
                        lstFile.Add(temp);
                    }
                }
            }
            lstFile.Sort();
            return lstFile;
        }

        private void LoadPlaceholder(ListItemCollection lstItems)
        {
            int portalId = Session.WorkingPortal().PORTALID;
            if (portalId == 0) portalId = Configurations.PORTALID;
            var infoPortal = ManagerFactory.DAOEntity.GetInfo(new PortalPortal() { PORTALID = portalId });
            if (infoPortal != null)
            {
                if (!string.IsNullOrEmpty(infoPortal.THEME))
                {
                    var infoTheme = JsonSerializer.DeserializeFromString<JTheme>(infoPortal.THEME);
                    if (infoTheme != null)
                    {
                        lstItems.Clear();
                        foreach (string row in infoTheme.placeholder)
                        {
                            var item = new ListItem(row, row);
                            lstItems.Add(item);
                        }
                    }
                }
            }
        }

        private void LoadControlItems(List<PortalControl> dtControls)
        {
            Module_drpControl.Items.Clear();
            

            List<string> lstDir = new List<string>();
            foreach (PortalControl rows in dtControls)
            {
                if (!string.IsNullOrEmpty(rows.CONTROLPATH))
                {
                    string[] arrDir = rows.CONTROLPATH.Split('/');
                    string dir = arrDir[1] + "/" + arrDir[2] + "/" + arrDir[3] + "/" + arrDir[4];
                    bool isExists = lstDir.Contains(dir);
                    if (!isExists)
                        lstDir.Add(dir);
                }
            }
            lstDir.Sort();
            foreach (var s in lstDir)
            {
                string dir = s.Split('/')[3];
                Module_drpControl.AddItemGroup(dir);
                List<PortalControl> lstControl = dtControls.FindAll(x => (x.CONTROLPATH.IndexOf(s) > 0));
                if (lstControl != null)
                {
                    foreach (PortalControl rows in lstControl)
                    {
                        var items = new ListItem(rows.CONTROLNAME, rows.CONTROLPATH);
                        Module_drpControl.Items.Add(items);
                    }
                }
            }
        }

        private void LoadSection()
        {
            int curMenutype = Convert.ToInt32(Module_drpMenutype.SelectedValue);
            List<JSection> lstSections = lstType[curMenutype].control;
            if (lstSections != null)
            {
                var lstModule = new List<JSection>();
                foreach (JSection portalSections in lstSections)
                {
                    lstModule.Add(portalSections);
                }
                rptModule.DataSource = (lstModule.Count > 0) ? lstModule : null;
                rptModule.DataBind();
            }
            else
            {
                rptModule.DataSource = null;
                rptModule.DataBind();
            }
        }

        private void LoadJavascript()
        {
            int curMenutype = Convert.ToInt32(Module_drpMenutype.SelectedValue);
            List<string> lstJavascripts = lstType[curMenutype].js;
            if (lstJavascripts != null)
            {
                rptJavascript.DataSource = (lstJavascripts.Count > 0) ? lstJavascripts : null;
                rptJavascript.DataBind();
            }
            else
            {
                rptJavascript.DataSource = null;
                rptJavascript.DataBind();
            }
        }

        private void LoadStyleSheet()
        {
            int curMenutype = Convert.ToInt32(Module_drpMenutype.SelectedValue);
            List<string> lstStyleSheets = lstType[curMenutype].css;
            if (lstStyleSheets != null)
            {
                rptStyleSheet.DataSource = (lstStyleSheets.Count > 0) ? lstStyleSheets : null;
                rptStyleSheet.DataBind();
            }
            else
            {
                rptStyleSheet.DataSource = null;
                rptStyleSheet.DataBind();
            }
        }
        
        protected void ActiveTab(object sender, EventArgs e)
        {
            var sends = sender as LinkButton;
            if (sends != null)
            {
                panelGeneral.Visible = false;
                btnGeneral.CssClass = "tab";
                panelType.Visible = false;
                btnType.CssClass = "tab";
                panelExtension.Visible = false;
                btnExtension.CssClass = "tab";
                panelModule.Visible = false;
                btnModule.CssClass = "tab";
                panelStyleSheet.Visible = false;
                btnStyleSheet.CssClass = "tab";                
                panelJavascript.Visible = false;
                btnJavascript.CssClass = "tab";
                switch (sends.CommandArgument)
                {
                    case "1":
                        panelGeneral.Visible = true;
                        btnGeneral.CssClass = "tab tabActive";
                        break;
                    case "2":
                        panelExtension.Visible = true;
                        btnExtension.CssClass = "tab tabActive";
                        break;
                    case "3":
                        panelType.Visible = true;
                        btnType.CssClass = "tab tabActive";
                        break;
                    case "4":
                        panelModule.Visible = true;
                        btnModule.CssClass = "tab tabActive";
                        break;
                    case "5":
                        panelJavascript.Visible = true;
                        btnJavascript.CssClass = "tab tabActive";
                        break;
                    case "6":
                        panelStyleSheet.Visible = true;
                        btnStyleSheet.CssClass = "tab tabActive";
                        break;
                    default:
                        break;
                }
            }
        }
    }
}